package cn.hfatec.shms.web.xh.xhCallback.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.web.xh.xhCallback.model.XhCallback;
import cn.hfatec.shms.web.xh.xhCallback.model.XhCallbackParams;
import cn.hfatec.shms.web.xh.xhCallback.service.XhCallbackService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/xh/xhCallback")
public class XhCallbackAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "XhCallbackService")
    private XhCallbackService xhCallbackService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(XhCallbackParams xhCallbackParams, Page page, Model model) {

        Table table = xhCallbackService.queryXhCallbackTable(xhCallbackParams, page);
        XhCallback xhCallback = xhCallbackService.getXhCallback(xhCallbackParams);
        model.addAttribute("xhCallbacksTable", table);
        model.addAttribute("xhCallbackHj", xhCallback);
        model.addAttribute("xhCallbackParams", xhCallbackParams);
        return "/xh/xhCallback/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(XhCallbackParams xhCallbackParams, Page page, Model model) {
        try {
            list(xhCallbackParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xh/xhCallback/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(XhCallbackParams xhCallbackParams, HttpServletRequest request, HttpServletResponse response) {
        try {
            xhCallbackService.exportExcel(xhCallbackParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

}
